import { createRouter, createWebHashHistory } from 'vue-router'
const routes = [{
    path: '/',
    name: 'Home',
    component:() => import(/* webpackChunkName: "home" */ '../views/home/Home')
  },{
    path: '/shop/:id',
    name: 'Shop',
    component:() => import(/* webpackChunkName: "shop" */ '../views/shop/Shop')
  },{
    path: '/register',
    name: 'Register',
    component:() => import(/* webpackChunkName: "shop" */'../views/register/Register'),
    beforeEnter: (to, from, next) => {
      const { isLogin } = localStorage;
      isLogin ?  next({name: 'Home'}) : next()
    }
  },{
    path: '/login',
    name: 'Login',
    component:() => import(/* webpackChunkName: "shop" */ '../views/login/Login'),
    beforeEnter: (to, from, next) => {
      const { isLogin } = localStorage;
      isLogin ?  next({name: 'Home'}) : next()
    }
}]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to, from, next) => {
   const { isLogin } = localStorage;
   const { name } = to;
   const isLoinOrReister = (name === 'Login' || name === 'Register');
   (isLogin || isLoinOrReister) ? next() :  next({name: 'Login'});
})

export default router
